package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.sercice.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //用户端验证码获取
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpSession session) {
        //获取用户手机号
        String phone = user.getPhone();
        //判断用户手机号是否为空
        if (StringUtils.isNotEmpty(phone)) {
            //不为空 再随机生成验证码
            //String code = ValidateCodeUtils.generateValidateCode4String(4);
            String code = RandomUtil.randomNumbers(4);
            log.info("验证码:----" + code);
            //调用工具类 ,发送短信
            //SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone,code);
            //将验证码存储到session中
            //session.setAttribute(phone, code);

            //将验证码存储到redis中
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set("code:" + phone, code, 5, TimeUnit.MINUTES);

            return R.success("验证码短信发送成功");
        }
        return R.error("验证码短信发送失败");

    }

    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpSession session) {
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();
        //从session 获取验证码
        //Object codeBySession = session.getAttribute(phone);

        //获取验证码
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object codeByRedis = valueOperations.get("code:" + phone);

        //进行验证码比对
        if (codeByRedis != null && codeByRedis.equals(code)) {
            //比对成功就登录成功
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, phone);
            User user = userService.getOne(wrapper);
            if (user == null) {
                //判断当前手机号的用户是否是新用户 ,如果是新用户就自动完成注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user", user.getId());

            //登录成功 删除验证码
            redisTemplate.delete("code:" + phone);

            return R.success(user);
        }
        return R.error("登录失败");
    }

    //退出登录
    @PostMapping("/loginout")
    public R loginOut(HttpSession session) {

        session.removeAttribute("user");

        return R.success("退出成功");
    }

}
